ArcPadScripting
Execute Example

Description

Demonstrates how to use the DataSource's Execute method and work with the returned array.

VBScript Code

Execute
Copy Code
Function OpenAXF(p_strAXFPath)
      Dim pDS
      Set pDS = Application.CreateAppObject("DataSource")
      pDS.Open(p_strAXFPath)
      If (pDS.IsOpen) Then
            Set OpenAXF = pDS
      Else
            Set OpenAXF = Nothing
      End If
End Function


Function GetFileNameUI(p_extension, p_filter, p_title, p_flags) 
      GetFileNameUI = CommonDialog.ShowOpen(p_extension, p_filter, p_title, p_flags)
      If (IsEmpty(GetFileNameUI)) Then
            GetFileNameUI = ""
      End If
End Function


Sub ExecuteStatement(p_SQL)
      '++ prompt for the AXF file
      Dim strFileName
      strFileName = GetFileNameUI("axf", "ArcPad AXF Files|*.axf","Select AXF File", &H1000)
      If ("" = strFileName) Then Exit Sub


      '++ open the selected AXF file
      Dim pDS
      Set pDS = OpenAXF(strFileName)
      If (pDS Is Nothing) Then
            Console.Print "Open DataSource failed"
            Exit Sub
      End If


      '++ execute the input SQL statement
      Dim pRS
      Set pRS = pDS.Execute(p_SQL)


      If (Not pRS Is Nothing) Then
            '++ one way of working with the returned RecordSet - 
'++ step through the records and output the first attribute to the Console window
            pRS.MoveFirst
            Do While Not pRS.EOF
                  Console.Print pRS.Fields(1).Name & ": " & pRS.Fields(1).Value
                  pRS.MoveNext
            Loop


            Console.Print "----------"


            '++ another way of working with the returned RecordSet -
            '++ convert it to a 2 dimensional array and output the attributes
            '++ row by row to the Console window
            Dim arrRecords, cols, rows
            arrRecords = pRS.ToArray
            For rows = 0 to UBound(arrRecords,1)
                  For cols = 0 to UBound(arrRecords,2)
                        Console.Print rows & "," & cols & ": " & arrRecords(rows,cols)
                  Next
            Next


            '++ close the RecordSet
            pRS.Close
            Set pRS = Nothing
      End If


      '++ close the DataSource
        pDS.Close
      Set pDS = Nothing
End Sub


'++ call ExecuteStatement
Console.Clear
Call ExecuteStatement("SELECT DISTINCT NAME,LAYERID FROM AXF_LAYERS")

JScript Code

 

Execute
Copy Code
function OpenAXF(p_strAXFPath)
{
      var pDS = Application.CreateAppObject("DataSource");
      pDS.Open(p_strAXFPath)
      if (pDS.IsOpen)
            return pDS
      else
            return null;
}
function GetFileNameUI(p_extension, p_filter, p_title, p_flags)
{
      var resOpen = CommonDialog.ShowOpen(p_extension, p_filter, p_title, p_flags);
      if (resOpen == null)
            return "";
      else
            return resOpen;
}
function ExecuteStatement(p_SQL)
{
      // prompt for the AXF file
      var strFileName = GetFileNameUI("axf", "ArcPad AXF Files|*.axf","Select AXF File", 0x1000);
      if ("" == strFileName)
            return;
      // open the selected AXF file
      var pDS = OpenAXF(strFileName);
      if (null == pDS)
      {
            Console.Print("Open DataSource failed");
            return;
      }
      //execute the input SQL statement
      var pRS = pDS.Execute(p_SQL);
      if (pRS != null)
      {
            // one way of working with the returned RecordSet - 
            //step through the records and output the first attribute to the Console window
            pRS.MoveFirst();
            while (!pRS.EOF)
            {
                  Console.Print(pRS.Fields(1).Name + ": " + pRS.Fields(1).Value);
                  pRS.MoveNext();
            }
            Console.Print("----------");
            // another way of working with the returned RecordSet -
            // convert it to a 2 dimensional array and output the attributes
            // row by row to the Console window
            var arrRecords = pRS.ToArray();
            var cols, rows;
            for (rows=arrRecords.lbound(1);rows<=arrRecords.ubound(1);rows++)
                  for(cols=arrRecords.lbound(2);cols<=arrRecords.ubound(2);cols++)
                        Console.Print(rows + "," + cols + ": " + arrRecords.getItem(rows,cols));
            // close the RecordSet
            pRS.Close();
            pRS = null;
      }
      //close the DataSource
        pDS.Close();
      pDS = null;
}
// call ExecuteStatement
Console.Clear();
ExecuteStatement("SELECT DISTINCT NAME,LAYERID FROM AXF_LAYERS");